﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Images;
using Silvermoon.Windows;
using Silvermoon.Shapes;
using Silvermoon.Navigation;
using Silvermoon.UI;

namespace OutlookHub.Pages
{
    public class TestView : Page
    {
        private ListView listView;
        private ListViewController<TextItem, string> controller;
        private List<string> values;
        private ContextMenu contextMenu;

        public TestView()
            : base()
        {
            Initialize();
        }

        protected override void Dispose(bool dispose)
        {
            base.Dispose(dispose);
            contextMenu.Dispose();
        }

        private void Initialize()
        {
            Title = "TestView";
            listView = new ListView { Margin = Margin.Zero };
            Controls.Add(listView);

            controller = new ListViewController<TextItem, string>(listView);
            controller.UpdateItem += (s, e) =>
                {
                    e.Item.Text = (e.Value as string);
                };

            controller.InitializeItem += (s, e) =>
                {
                    e.Item.ContextMenu = contextMenu;
                    e.Item.Tap += new EventHandler<TapEventArgs>(DeleteItem);
                    e.Item.Background = new RoundedEdgeShape { Color = PhoneColors.Accent };
                    e.Item.Margin = new Margin(4);
                    //                    e.Item.Chrome = new BorderShape { BorderSize = 2, Color = PhoneColors.White };
                };
            contextMenu = new ContextMenu();
            contextMenu.MenuItems = new Item[] {
                new TextItem("MenuItem 1"),
                new TextItem("MenuItem 2"),
                new TextItem("MenuItem 3"),
                new TextItem("MenuItem 4"),
            };

            Toolbar tb = new Toolbar();
            ToolbarButton btn1 = new ToolbarButton { Content = SysImages.RecycleBin };
            ToolbarButton btn2 = new ToolbarButton { Content = SysImages.Add };
            ToolbarButton btn3 = new ToolbarButton { Content = SysImages.Check };
            tb.Buttons.AddRange(btn1, btn2, btn3);
            btn1.Tap += (s, e) =>
            {
                if (values.Count > 2) controller.RemoveValue(values[2]);
            };

            btn2.Tap += (s, e) =>
            {
                controller.InsertValue(2, "Hello " + Environment.TickCount.ToString());
                //values.Insert(2, "Hello " + Environment.TickCount.ToString()); listView.OnItemInserted(2);
            };

            btn3.Tap += (s, e) =>
                {
                    listView.BringItemIntoView(19, true);
                };

            Toolbar = tb;

            values = new List<string>(100);
            for (int i = 1; i < 100; i++)
            {
                string s = "Item #" + i.ToString();
                values.Add(s);
            }
            controller.DataSource = values;
        }

        void DeleteItem(object sender, TapEventArgs e)
        {
            Item item = sender as Item;
            controller.RemoveValue(item.DataValue as string);
        }

        protected override void OnHome(HandledEventArgs e)
        {
            SetTransition(350, TransitionMask.RotateLeftIn, 350, TransitionMask.RotateLeftOut);
            base.OnHome(e);
        }

    }
}
